Completed
Pull Request — master (#79)
by
unknown
35s
created

boilerplate.js ➔ getInfoFiles   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
import fs from 'fs';
2
import path from 'path';
3
import process from 'process';
4
import { all, promisify, reject } from 'bluebird';
5
import {
6
    T,
7
    append,
8
    cond,
9
    dropWhile,
10
    equals,
11
    flatten,
12
    join,
13
    juxt,
14
    last,
15
    map,
16
    merge,
17
    pick,
18
    prop,
19
    replace,
20
    split,
21
    startsWith
22
} from 'ramda';
23
import semver from 'semver';
24
import superagent from 'superagent';
25
import inquirer from 'inquirer';
26
import { emitSuccess } from './input';
27
28
const request = superagent.agent();
29
const createFolder = promisify(fs.mkdir);
30
const createFile = promisify(fs.writeFile);
31
32
/**
33
 * Formats a formatted String
34
 *
35
 * @param {String} source
36
 * @return {String}
37
 */
38
const format = replace(/\n {8}/g, '\n')
39
    & dropWhile(equals('\n'))
40
    & append('\n')
41
    & join('');
42
43
/**
44
 * Returns the default name for the project according to the OS
45
 *
46
 * @param {String} directory
47
 * @return {String}
48
 */
49
const getDefaultName = cond([
50
    [~startsWith('win32', process.platform), split('\\')],
51
    [T, split('/')]
52
]) & last;
53
54
/**
55
 * Generate the answers from the stdin.
56
 *
57
 * @return {Promise}
58
 */
59
function askQuestions() {
60
    return request.get('https://app.rung.com.br/api/categories')
61
        .then(prop('body') & map(({ name, alias: value }) => ({ name, value })))
0 ignored issues
show
Bug introduced by
The variable value seems to be never declared. If this is a global, consider adding a /** global: value */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The parameter alias is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
62
        .catch(~[{ name: 'Miscellaneous', value: 'miscellaneous' }])
63
        .then(categories => [
64
            { name: 'name', message: 'Project name', default: getDefaultName(process.cwd()) },
65
            { name: 'version', message: 'Version', default: '1.0.0', validate: semver.valid & Boolean },
66
            { name: 'title', message: 'Title', default: 'Untitled' },
67
            { name: 'description', message: 'Description' },
68
            { name: 'category', type: 'list', message: 'Category', default: 'miscellaneous', choices: categories },
69
            { name: 'license', message: 'license', default: 'MIT' }
70
        ])
71
        .then(inquirer.createPromptModule());
72
}
73
74
/**
75
 * Creates the folder for the boilerplate based on package name. If the folder
76
 * already exists, throw an error
77
 * Queria estar morta
78
 *
79
 * @param {Object} answers
80
 * @return {Promise}
81
 */
82
function createBoilerplateFolder(answers) {
83
    return createFolder(answers.name)
84
        .then(createFolder(`${answers.name}/info`))
85
        .catch(~reject(new Error(`Unable to create folder ${answers.name}`)))
86
        .return(answers);
87
}
88
89
/**
90
 * Returns an object in the format { filename :: String, content :: String }
91
 * containing meta-informations about the file
92
 *
93
 * @param {Object} answers
94
 * @return {Object}
95
 */
96
function getPackageMetaFile(answers) {
97
    const packageFields = ['name', 'version', 'license', 'category'];
98
    const packageObject = merge(pick(packageFields, answers),
99
        { dependencies: { 'rung-cli': '1.0.0' } });
100
101
    return {
102
        filename: path.join(answers.name, 'package.json'),
103
        content: JSON.stringify(packageObject, null, 2)
104
    };
105
}
106
107
/**
108
 * Content about README.md file
109
 *
110
 * @param {Object} answers
111
 * @return {Object}
112
 */
113
function getReadMeMetaFile(answers) {
114
    const content = format(`
115
        # Rung ─ ${answers.title}
116
117
        # Development
118
119
        - Use \`yarn\` to install the dependencies
120
        - Use \`rung run\` to start the CLI wizard
121
    `);
122
123
    return { filename: path.join(answers.name, 'README.md'), content };
124
}
125
126
/**
127
 * Content about info files
128
 *
129
 * @param {Object} answers
130
 * @return {Object}
131
 */
132
function getInfoFiles(answers) {
133
    return [
134
        { filename: path.join(answers.name, 'info/en.md'), content: '' },
135
        { filename: path.join(answers.name, 'info/es.md'), content: '' },
136
        { filename: path.join(answers.name, 'info/pt_BR.md'), content: '' }];
137
}
138
139
/**
140
 * Content about index.js file
141
 *
142
 * @param {Object} answers
143
 * @return {Object}
144
 */
145
function getIndexFile(answers) {
146
    const content = format(`
147
        import { create } from 'rung-sdk';
148
        import { String as Text } from 'rung-cli/dist/types';
149
150
        function render(name) {
151
            return <b>{ _('Hello {{name}}', { name }) }</b>;
152
        }
153
154
        function main(context) {
155
            const { name } = context.params;
156
            return {
157
                alerts: [{
158
                    title: _('Welcome'),
159
                    content: render(name),
160
                    resources: []
161
                }]
162
            };
163
        }
164
165
        const params = {
166
            name: {
167
                description: _('What is your name?'),
168
                type: Text
169
            }
170
        };
171
172
        export default create(main, {
173
            params,
174
            primaryKey: true,
175
            title: _(${JSON.stringify(answers.title)}),
176
            description: _(${JSON.stringify(answers.description)}),
177
            preview: render('Trixie')
178
        });
179
    `);
180
181
    return { filename: path.join(answers.name, 'index.js'), content };
182
}
183
184
/**
185
 * Creates a boilerplate project
186
 *
187
 * @return {Promise}
188
 */
189
export default function boilerplate() {
190
    return askQuestions()
191
        .then(createBoilerplateFolder)
192
        .then(juxt([getPackageMetaFile, getReadMeMetaFile, getIndexFile, getInfoFiles]))
193
        .then(flatten)
194
        .then(map(({ filename, content }) => createFile(filename, content)) & all)
195
        .then(~emitSuccess('project generated'));
196
}
197